<template>
  <div class="login">
    <div class="login_box">
      <div class="login_logo">UNI-ADMIN</div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="button">
        <el-button type="primary" @click="onClick">登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    onClick() {
      axios
        .post(
          "http://ceshi5.dishait.cn/admin/login",
          {
            username: this.ruleForm.username,
            password: this.ruleForm.password,
          }
        )
        .then((res) => {
          console.log(res);
          var token = res.data.data.token;

          window.localStorage.setItem("token", token);

          this.$router.push("/");
          this.$message("登录成功");
        })
        .catch((error) => {
          this.$message.error("登陆失败");
          console.log(error);
        });
    },
  },
};
</script>

<style>
.login {
  width: 100%;
  height: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.demo-ruleForm {
  width: 400px;
  margin-top: 20px;
}
.login_box {
  width: 500px;
  height: 300px;
  background: white;
  border: 1px solid gray;
}
.login_logo {
  width: 100%;
  height: 60px;
  border: 1px solid gray;
  font-size: 26px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}
.button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.el-button {
  width: 320px;
}
</style>